package com.ruoyi.sycs.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.ruoyi.common.helper.LoginHelper;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import liquibase.pro.packaged.P;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.sycs.domain.bo.BaQualDetailBo;
import com.ruoyi.sycs.domain.vo.BaQualDetailVo;
import com.ruoyi.sycs.domain.BaQualDetail;
import com.ruoyi.sycs.mapper.BaQualDetailMapper;
import com.ruoyi.sycs.service.IBaQualDetailService;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 判定依据-技术指标详细Service业务层处理
 *
 * @author zyb
 * @date 2024-04-08
 */
@RequiredArgsConstructor
@Service
public class BaQualDetailServiceImpl implements IBaQualDetailService {

    private final BaQualDetailMapper baseMapper;

    /**
     * 查询判定依据-技术指标详细
     */
    @Override
    public BaQualDetailVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询判定依据-技术指标详细列表
     */
    @Override
    public TableDataInfo<BaQualDetailVo> queryPageList(BaQualDetailBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<BaQualDetail> lqw = buildQueryWrapper(bo);
        Page<BaQualDetailVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询判定依据-技术指标详细列表
     */
    @Override
    public List<BaQualDetailVo> queryList(BaQualDetailBo bo) {
        LambdaQueryWrapper<BaQualDetail> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<BaQualDetail> buildQueryWrapper(BaQualDetailBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<BaQualDetail> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getRefQualId() != null, BaQualDetail::getRefQualId, bo.getRefQualId());
        lqw.eq(bo.getJudgeBsId() != null, BaQualDetail::getJudgeBsId, bo.getJudgeBsId());
        lqw.eq(StringUtils.isNotBlank(bo.getSymbolValue()), BaQualDetail::getSymbolValue, bo.getSymbolValue());
        lqw.eq(StringUtils.isNotBlank(bo.getNum1()), BaQualDetail::getNum1, bo.getNum1());
        lqw.eq(StringUtils.isNotBlank(bo.getNum1()), BaQualDetail::getNum2, bo.getNum2());
        return lqw;
    }

    /**
     * 新增判定依据-技术指标详细
     */
    @Override
    public Boolean insertByBo(BaQualDetailBo bo) {
        BaQualDetail add = BeanUtil.toBean(bo, BaQualDetail.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改判定依据-技术指标详细
     */
    @Override
    public Boolean updateByBo(BaQualDetailBo bo) {
        BaQualDetail update = BeanUtil.toBean(bo, BaQualDetail.class);
        validEntityBeforeSave(update);
        // 如果符号是范围类型,则更新num2的值
        if (!(update.getSymbolValue().equals("07") || update.getSymbolValue().equals("08") || update.getSymbolValue().equals("09") || update.getSymbolValue().equals("10"))) {
            UpdateWrapper wrapper = new UpdateWrapper();
            wrapper.set("num2", null);
            wrapper.set("num1", update.getNum1());
//            wrapper.set("ref_qual_id", update.getRefQualId());
            wrapper.set("judge_bs_id", update.getJudgeBsId());
            wrapper.set("symbol_value", update.getSymbolValue());
            wrapper.set("remark", update.getRemark());
            wrapper.set("update_by", LoginHelper.getUsername());
            wrapper.set("update_time", new Date());
            wrapper.eq("id", bo.getId());
            return baseMapper.update(update, wrapper) > 0;
        } else {
            return baseMapper.updateById(update) > 0;
        }
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(BaQualDetail entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除判定依据-技术指标详细
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }


    /**
     * 根据bsId查询明细
     * @param bo
     * @param pageQuery
     * @return
     */
    @Override
    public IPage pageByBsId(BaQualDetailBo bo, PageQuery pageQuery) {
        if (ObjectUtils.isNotEmpty(bo.getJudgeBsId())) {
            IPage<BaQualDetailVo> page = baseMapper.pageByBsId(bo, pageQuery.build());
            return page;
        } else {
            return new Page();
        }

    }
}
